package com.astronomy.base.service;

import com.astronomy.base.mapper.CityMapper;
import com.astronomy.base.pojo.City;
import com.astronomy.entity.IdWorker;
import com.astronomy.entity.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CityService {

    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 全部数据
     * @return
     */
    public List<City> findAll(){
        return cityMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public City findById(String id){
        return  cityMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加
     * @param city
     */
    public void add(City city){
        city.setId(idWorker.nextId()+"");
        cityMapper.insert(city);
    }

    /**
     * 修改
     * @param city
     */
    public void update(City city){
        cityMapper.updateByPrimaryKey(city);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id){
        cityMapper.deleteByPrimaryKey(id);
    }


    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    public Page<City> findPage(int page, int size){
        PageHelper.startPage(page,size);
        return (Page<City>)cityMapper.selectAll();
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    public List<City> findList(Map<String, Object> searchMap){
        Example example = createExample(searchMap);
        return cityMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(City.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // ID
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
           	}
            // 城市名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
           	}
            // 是否热门
            if(searchMap.get("ishot")!=null && !"".equals(searchMap.get("ishot"))){
                criteria.andLike("ishot","%"+searchMap.get("ishot")+"%");
           	}


        }
        return example;
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    public PageResult<City> findPage(Map<String, Object> searchMap, int page, int size){
        //查询条件
        Example example = createExample(searchMap);
        //开始分页
        PageHelper.startPage(page,size);
        //查询结果
        List<City> cities = cityMapper.selectByExample(example);
        //包装成pageInfo
        PageInfo<City> pageInfo=new PageInfo<>(cities);
        //返回结果
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

}
